package com.pronhub.cloud.shop.mapper;

import com.pronhub.cloud.shop.domain.SeckillGood;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface SeckillGoodMapper {
    @Select("select * from t_seckill_goods ")
    List<SeckillGood> query();
    @Select("select * from t_seckill_goods where id = #{seckillId}")
    SeckillGood get(Long seckillId);


    @Update("update t_seckill_goods set stock_count = stock_count - 1 where id = #{seckillId} and stock_count > 0 ")
        //方法的返回值影响行数，如果返回值>0 说明执行成功了，库存-1  如果返回值为0，说明这条语句执行了，但是没有修改到数据（没有库存）
    //单纯利用了MyBatis数据库的Sql语句乐观锁来确定不能超卖的
    int decrSeckillCountBySeckillId(Long seckillId);
    @Select("select stock_count from t_seckill_goods where id = #{seckillId}")
    int getStockCount(Long seckillId);

    @Update("update t_seckill_goods set stock_count = stock_count + 1 where id = #{seckillId}")
    void incrStock(Long seckillId);
}
